package com.fog.judge.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fog.judge.common.Base;
import com.fog.judge.common.UserContext;
import com.fog.judge.dto.CourseTeacherDto;
import com.fog.judge.dto.TeamTeacherDto;
import com.fog.judge.entity.*;
import com.fog.judge.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/qualitativeEvaluations")
public class QualitativeEvaluationsController {

    @Resource
    private QualitativeEvaluationsService qualitativeEvaluationsService;

    @Resource
    private UserService userService;

    @Resource
    private CourseService courseService;

    @Resource
    private SelectCourseService selectCourseService;

    @Resource
    private TeamsService teamsService;

    @Resource
    private TeacherService teacherService;

    @PostMapping("/calculateTeachingQuality")
    public Base calculateTeachingQuality(@RequestBody Map<String,Integer> params){

        Integer teacherId = params.get("teacherId");
        int userId = UserContext.getUserId();
        User user = userService.findUserById(userId);

        boolean hasEvaluator = qualitativeEvaluationsService.hasEvaluator(userId, teacherId);
        if(hasEvaluator){
            return Base.error("当前用户已经评价过该教师");
        }

        if(user.getRole()==1){
            Integer TeachingMethod = params.get("teachingMethod");    // 教学方法 1-5
            Integer ClassroomInteraction = params.get("classroomInteraction");    // 课堂互动 1-5
            Integer StudentEvaluation = params.get("studentEvaluation");      // 学生总评 1-5
            Integer score = TeachingMethod+ClassroomInteraction+StudentEvaluation;

            QualitativeEvaluations qualitativeEvaluations = new QualitativeEvaluations();
            qualitativeEvaluations.setEvalutorId(userId);
            qualitativeEvaluations.setTeacherId(teacherId);
            qualitativeEvaluations.setEvalutionType(1);
            qualitativeEvaluations.setEvalutionScore(score);
            qualitativeEvaluations.setEvalutionDate(LocalDateTime.now());
            qualitativeEvaluationsService.save(qualitativeEvaluations);
        }else if(user.getRole()==2){
            Integer TeachingActivities = params.get("teachingActivities");  // 教学活动质量 1-5
            Integer AllocationOfStudyTime = params.get("allocationOfStudyTime"); // 学时分配 1-5
            Integer ResourceContribution = params.get("resourceContribution");  // 资源贡献 1-5
            Integer score = TeachingActivities + AllocationOfStudyTime + ResourceContribution;

            QualitativeEvaluations qualitativeEvaluations = new QualitativeEvaluations();
            qualitativeEvaluations.setEvalutorId(userId);
            qualitativeEvaluations.setTeacherId(teacherId);
            qualitativeEvaluations.setEvalutionType(2);
            qualitativeEvaluations.setEvalutionScore(score);
            qualitativeEvaluations.setEvalutionDate(LocalDateTime.now());
            qualitativeEvaluationsService.save(qualitativeEvaluations);
        }
        return Base.success("评价完毕");
    }

    @GetMapping("/studentevaluate")
    public Base studentEvaluate(int page, int pageSize){
        Page<CourseTeacherDto> pageInfo = courseService.selectSearch(page,pageSize);
        return Base.success(pageInfo);
    }

    @GetMapping("/teachervaluate")
    public Base teacherValuate(int page, int pageSize){
        Page<TeamTeacherDto> pageInfo = teamsService.teamSearch(page,pageSize);
        return Base.success(pageInfo);
    }

}
